package com.seek.inter;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.seek.cache.CacheUtil;
import com.seek.common.handler.SpringContextHolder;
import com.seek.model.dto.ResultInfo;
import com.seek.service.ConfigService;

/**
 * t_sysconfig相关接口
 *
 */
public class SysConfigInter {
	private static Logger logger = LoggerFactory.getLogger(SysConfigInter.class);
	private ConfigService configService = (ConfigService)SpringContextHolder.getBean("configService");
	
	/**
	 * 获取配置参数列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ResultInfo getConfigParamList(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		String companyId = reqJson.getString("companyId");
		
		String sql = "select id paramId, remark paramName, value paramValue from t_config where company_id=? and is_used=1 order by id asc";
		List<Map<String,Object>> data = configService.findMapBySql(sql, new Object[]{companyId});
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(data);
		return result;
	}
	
	/**
	 * 保存参数配置选项
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ResultInfo saveConfigParam(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		List<JSONObject> data = (List<JSONObject>)reqJson.get("data");
		if (companyId == null || companyId < 1 || data == null || data.size() < 1){
			result.setStatus(1);
			result.setMessage("参数不完整，请检查！"); 
			return result;
		}
		try{
			configService.saveConfigBatch(data);
			CacheUtil.delRedisData("SYSCONFIG_"+companyId);
			result.setStatus(0);
			result.setMessage("成功");
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
}
